/*
 * 上海远境文化传媒有限公司 版权所有
 */
package controllers.admin;

import admin.*;
import controllers.auth.Secure;
import controllers.baseAPI.BaseJsonAPI;
import enums.DeletedStatus;
import org.apache.commons.lang.StringUtils;
import param.admin.AdminRoleRequest;
import play.Logger;
import play.data.validation.Required;
import play.i18n.Messages;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.With;
import response.admin.AdminRoleDTO;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色接口.
 */
//@With(Secure.class)
public class AdminRoleCtrl extends BaseJsonAPI {



    public static final Integer PAGE_SIZE = 10;

    public static void index(String key, Integer page ,Integer pageSize, String field, String order) {
        Map<String, Object> searchMap = new HashMap<>();
        page = page == null ? 1 : page;
        pageSize = pageSize == null? PAGE_SIZE :pageSize;

        String orderBy = "createdAt desc";
        if(!StringUtils.isEmpty(field)) {
            if (StringUtils.isEmpty(order)) order = "asc";
            orderBy = field+" "+order;
        }

        if (StringUtils.isNotBlank(key))
            searchMap.put("key", "'%" + key + "%'");

        JPAExtPaginator pageList = AdminRole.findByCondition(searchMap, orderBy, page, pageSize);
        renderPaginator(pageList);

    }

    /**
     * 显示详情.
     */
    public static void show(String id) {
        AdminRole role = AdminRole.findByRoleId(id);
        if (role == null)
            renderWarnMessage(403, "角色不存在，可能已经被删除！");
        List<AdminRoleOperate> adminRoleOperateList = AdminRoleOperate.findByRoleId(role.id);
        String[] operateIds = new String[adminRoleOperateList.size()];
        for(int i = 0; i<adminRoleOperateList.size();i++){
            operateIds[i] = adminRoleOperateList.get(i).id+"";
        }
        AdminRoleDTO response = AdminRoleDTO.from(role);
        renderData(response);
    }



    /**
     * 删除角色.
     */
    public static void delete(@Valid @Required String id) {
        AdminRole role = AdminRole.findById(id);
        if (role == null)
            renderWarnMessage(403, Messages.get("warn.no-exists", "角色"));
        Long total = AdminUserRole.countByRole(id);
        if(total>0){
            renderWarnMessage(403, Messages.get("role.delete-with-users", total,role.name));
        }
        role.deleted = DeletedStatus.DELETED ;
        role.modifyAt = new Date();
        role.save();
        flash.put("notice", Messages.get("flash.notice.delete-success", "角色"));
        renderSuccMessage("删除成功！");

    }

    /**
     * 创建新Role.
     */
    public static void create() {
        AdminRoleRequest roleRequest = validateRequestBody("role", AdminRoleRequest.class);
        if(roleRequest == null)
            renderWarnMessage(403, Messages.get("warn.no-exists", "角色"));
        AdminUser loginUser = Secure.getLoginUser();
        AdminRole role = AdminRole.create(roleRequest , loginUser);
        flash.put("notice", Messages.get("flash.notice.save-success"));
        renderData(AdminRoleDTO.from(role));
    }

    /**
     * 编辑Role.
     */
    public static void update(@Valid @Required String id) {
        AdminRoleRequest request = validateRequestBody("role", AdminRoleRequest.class);
        if(request == null){
            renderWarnMessage(403, Messages.get("warn.not-exists","表单参数"));
        }
        AdminRole role = AdminRole.findByRoleId(id);
        if(role == null){
            renderWarnMessage(403, Messages.get("warn.not-exists","角色"));
        }
        notFoundIfNull(role);

        AdminUser loginUser = Secure.getLoginUser();
        role = AdminRole.update(role,request , loginUser);
        flash.put("notice", Messages.get("flash.notice.save-success"));
        renderData(AdminRoleDTO.from(role));
    }





}
